<template>
  <div class="mod-user">
    <el-form :inline="true"  :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.orderNo" :placeholder="$t('message.orderNo')" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="dataForm.email" :placeholder="$t('message.email')" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">{{$t('message.search')}}</el-button>
        <el-button @click="clearQueryData()">{{$t('message.clean')}}</el-button>
        <el-button type="primary" v-show="isAdmin === 0" @click="AddHandle()" >{{$t('message.add')}}</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        prop="orderNo"
        header-align="center"
        align="center"
        :label="$t('message.orderNo')">
      </el-table-column>
      <el-table-column
        prop="userName"
        header-align="center"
        align="center"
        :label="$t('message.dealerName')">
      </el-table-column>
      <el-table-column
        prop="company"
        header-align="center"
        align="center"
        :label="$t('message.company')">
      </el-table-column>
      <el-table-column
        prop="email"
        header-align="center"
        align="center"
        :label="$t('message.dealerEmail')">
      </el-table-column>
      <el-table-column
        prop="mobile"
        header-align="center"
        align="center"
        :label="$t('message.phoneNo')">
      </el-table-column>
      <el-table-column
          prop="status"
          header-align="center"
          align="center"
          :label="$t('message.status')">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 0" size="small" type="danger">{{$t('message.noSubmit')}}</el-tag>
          <el-tag v-if="scope.row.status === 1" size="small" type="danger">{{$t('message.noConfirm')}}</el-tag>
          <el-tag v-else-if="scope.row.status === 2" size="small" type="success">{{$t('message.confirmed')}}</el-tag>
          <el-tag v-else-if="scope.row.status === 3" size="small" type="success">{{$t('message.shipped')}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        :label="$t('message.createTime')">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        :label="$t('message.operation')">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id, 0)">{{$t('message.read')}}</el-button>
          <!-- 经销商在未提交的时候才可以修改、删除、提交 -->
          <el-button type="text" size="small" v-show="isAdmin == 0 && scope.row.status === 0" @click="deleteHandle(scope.row.id)">{{$t('message.del')}}</el-button>
          <el-button type="text" size="small" v-show="isAdmin == 0 && scope.row.status === 0" @click="updateOrderHandle(scope.row.id, 1)">{{$t('message.submit')}}</el-button>
          <!-- 供应商待确认的时候可以确认，已确认待发货的时候可以发货 -->
          <el-button type="text" size="small" v-show="isAdmin == 1 && scope.row.status === 1" @click="updateOrderHandle(scope.row.id, 2)">{{$t('message.confirm')}}</el-button>
          <el-button type="text" size="small" v-show="isAdmin == 1 && scope.row.status === 2" @click="shipOrderHandle(scope.row.id)">{{$t('message.ship')}}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <add-order v-if="addOrderVisible" ref="AddOrder" @refreshDataList="getDataList"></add-order>
    <ship-order v-if="shipOrderVisible" ref="ShipOrder" @refreshDataList="getDataList"></ship-order>
  </div>
</template>

<script>
  import AddOrUpdate from './dealer-purchase-info-add-or-update'
  import AddOrder from './dealer-add-order'
  import ShipOrder from './dealer-ship-order'

  export default {
    data () {
      return {
        isAdmin: 0,
        dataForm: {
          id: '',
          status: '',
          comment: '',
          orderNo: '',
          email: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        addOrderVisible: false,
        shipOrderVisible: false
      }
    },
    components: {
      AddOrUpdate,
      AddOrder,
      ShipOrder
    },
    activated () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/sys/sysOrder/list'),
          method: 'POST',
          data: this.$http.adornData({
            'currPage': this.pageIndex,
            'limit': this.pageSize,
            'orderNo': this.dataForm.orderNo,
            'email': this.dataForm.email
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
            this.isAdmin = data.isAdmin
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 表单提交
      updateOrderHandle (orderId, status) {
        this.$http({
          url: this.$http.adornUrl('/sys/sysOrder/updateOrderStatus'),
          method: 'post',
          data: this.$http.adornData({
            'orderId': orderId,
            'status': status
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: this.$t('message.success'),
              type: 'success',
              duration: 300,
              onClose: () => {
                this.visible = false
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      },
      addOrUpdateHandle (id, opt) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id, opt)
        })
      },
      AddHandle () {
        this.addOrderVisible = true
        this.$nextTick(() => {
          this.$refs.AddOrder.init()
        })
      },
      shipOrderHandle (id) {
        this.shipOrderVisible = true
        this.$nextTick(() => {
          this.$refs.ShipOrder.init(id)
        })
      },
      // 删除
      deleteHandle (id) {
        this.$confirm(this.$t('message.confirmOpt'), this.$t('message.prompt'), {
          confirmButtonText: this.$t('message.confirm'),
          cancelButtonText: this.$t('message.cancle'),
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/sys/sysOrder/delete'),
            method: 'post',
            data: this.$http.adornData({
              'orderId': id
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: this.$t('message.success'),
                type: 'success',
                duration: 500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }).catch(() => {})
      },
      clearQueryData () {
        this.dataForm.orderNo = ''
        this.dataForm.email = ''
        this.getDataList()
      }
    }
  }
</script>
